package com.core.start.controller;

import com.core.dto.CommandRequestDTO;
import com.core.exception.BizException;
import com.core.handler.CommandHandler;
import com.core.registrar.CommandRegistry;
import com.core.result.CmdResult;
import org.noear.solon.annotation.*;
import org.noear.solon.validation.annotation.Validated;

import java.util.Map;

/**
 * 统一入口
 * @author hc
 * @CreateTime 2025/7/29 10:44
 */
@Controller
public class CommandController {

    @Inject("CommandRegistry")
    CommandRegistry registry;

    @Post
    @Mapping("/api/entry")
    public CmdResult<Object> entry(@Validated @Body CommandRequestDTO body) {
        String command = body.getCommand();
        Map<String, Object> params = body.getParams();

        CommandHandler handler = registry.getHandler(command);
        if (handler == null) {
            throw new BizException("命令未注册：" + command);
        }
        Object result = handler.execute(params);
        return CmdResult.ok(result);
    }
}
